import psycopg2
import psycopg2.extras
from config import Config

class Model(object):

    db = None
    cursor = None

    def __init__(self):
        self.db = psycopg2.connect("dbname='" + Config.DB_NAME + "' user='" + Config.DB_USER + "' host='" + Config.DB_HOST + "' password='" + Config.DB_PASSWORD + "'")
        self.cursor = self.db.cursor(cursor_factory=psycopg2.extras.DictCursor)
    
    def __del__(self):
        self.db.commit()
        self.db.close()

class Zone(Model):

    def get(self, id):
        self.cursor.execute("""SELECT * FROM dnsadmin_zone WHERE id=%(id)s""", dict(id=id))
        return self.cursor.fetchone()

    def list(self):
        self.cursor.execute("""SELECT * FROM dnsadmin_zone ORDER BY name""")
        return self.cursor.fetchall()
        
    def list_enabled(self):
        self.cursor.execute("""SELECT * FROM dnsadmin_zone WHERE enabled ORDER BY name""")
        return self.cursor.fetchall()
        
    def get_by_name(self, name):
        self.cursor.execute("""SELECT * FROM dnsadmin_zone WHERE name=%(name)s""", {"name" : name})
        return self.cursor.fetchone()

    def add(self, name, forward_lookup, serial_number, refresh_time, retry_time, expire_time, minimum_ttl, source_host, contact_email, enabled):
        dict_data_zone = {
            "name": name,
            "forward_lookup" : forward_lookup,
            "serial_number" : serial_number,
            "refresh_time" : refresh_time,
            "retry_time" : retry_time,
            "expire_time" : expire_time,
            "minimum_ttl" : minimum_ttl,
            "source_host" : source_host,
            "contact_email" : contact_email,
            "enabled" : enabled
        }
        self.cursor.execute("""INSERT INTO dnsadmin_zone (forward_lookup, name, serial_number, refresh_time, retry_time, expire_time, minimum_ttl, source_host, contact_email, creation, enabled)
VALUES (%(forward_lookup)s, %(name)s, %(serial_number)s, %(refresh_time)s, %(retry_time)s, %(expire_time)s, %(minimum_ttl)s, %(source_host)s, %(contact_email)s, NOW(), %(enabled)s)""", dict_data_zone)

    def remove(self, id):
        self.cursor.execute("DELETE FROM dnsadmin_zone WHERE id=%(id)s", dict(id=id))
        
    def update(self, id, forward_lookup, serial_number, refresh_time, retry_time, expire_time, minimum_ttl, source_host, contact_email, enabled):
        dict_data_zone = {
            "serial_number": serial_number,
            "refresh_time": refresh_time,
            "retry_time": retry_time,
            "expire_time": expire_time,
            "minimum_ttl": minimum_ttl,
            "source_host": source_host,
            "contact_email": contact_email,
            "forward_lookup": forward_lookup,
            "zone_id": id,
            "enabled": enabled
        }
        self.cursor.execute("""UPDATE dnsadmin_zone SET serial_number=%(serial_number)s, refresh_time=%(refresh_time)s, retry_time=%(retry_time)s, expire_time=%(expire_time)s, minimum_ttl=%(minimum_ttl)s, source_host=%(source_host)s, contact_email=%(contact_email)s, enabled=%(enabled)s, forward_lookup=%(forward_lookup)s WHERE id=%(zone_id)s""", dict_data_zone)
    
class Record(Model):

    def get(self, id):
        self.cursor.execute("SELECT *, (SELECT name FROM dnsadmin_recordclass WHERE id=dnsadmin_record.record_class_id) AS record_class  FROM dnsadmin_record WHERE id=%(id)s", dict(id=id))
        return self.cursor.fetchone()
    def remove(self, id):
        self.cursor.execute("DELETE FROM dnsadmin_record WHERE id=%(id)s", dict(id=id))

    def list(self, zone_id):
        self.cursor.execute("SELECT *, (SELECT name FROM dnsadmin_recordclass WHERE id=dnsadmin_record.record_class_id) AS record_class FROM dnsadmin_record WHERE zone_id=%(zone)s ORDER BY id", dict(zone=zone_id))
        return self.cursor.fetchall()
        
    def list_enabled(self, zone_id):
        self.cursor.execute("SELECT *, (SELECT name FROM dnsadmin_recordclass WHERE id=dnsadmin_record.record_class_id) AS record_class FROM dnsadmin_record WHERE enabled AND zone_id=%(zone)s ORDER BY id", dict(zone=zone_id))
        return self.cursor.fetchall()

    def remove_by_zone_id(self, zone_id):
        self.cursor.execute("DELETE FROM dnsadmin_record WHERE zone_id=%(zone)s", dict(zone=zone_id))

    def add(self, name, value, record_class, enabled, zone_id):
        record_class_dict = {"NS": 1, "A": 2, "CNAME": 3, "PTR": 4}
        data_record_dict = {
            "name": name,
            "value": value,
            "zone": str(zone_id),
            "enabled": enabled,
            "record_class": record_class_dict[record_class]
    	}
        self.cursor.execute("""INSERT INTO dnsadmin_record (name, value, zone_id, record_class_id, enabled, creation) VALUES (%(name)s, %(value)s, %(zone)s, %(record_class)s, %(enabled)s, NOW())""", data_record_dict)
        
    def change(self, id, name, value, record_class, enabled, zone_id):
        record_class_dict = {"NS": 1, "A": 2, "CNAME": 3, "PTR": 4}
        data_record_dict = {
            "name": name,
            "value": value,
            "zone": str(zone_id),
            "enabled": enabled,
            "record_class": record_class_dict[record_class],
            "id": id
    	}
        self.cursor.execute("""UPDATE dnsadmin_record SET name=%(name)s, value=%(value)s, zone_id=%(zone)s, record_class_id=%(record_class)s, enabled=%(enabled)s WHERE id=%(id)s""", data_record_dict)